package com.zhku.goods.service.jobhandler;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zhku.base.exception.TaoTaoException;
import com.zhku.goods.feignclient.GoodsIndex;
import com.zhku.goods.feignclient.SearchServiceClient;
import com.zhku.goods.mapper.TtGoodsMapper;
import com.zhku.goods.model.po.TtGoods;
import com.zhku.goods.service.TtGoodsService;
import com.zhku.messagesdk.model.po.MqMessage;
import com.zhku.messagesdk.service.MessageProcessAbstract;
import com.zhku.messagesdk.service.MqMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 商品发布任务类
 */
@Slf4j
@Component
public class GoodsPublishTask extends MessageProcessAbstract {

    @Autowired
    TtGoodsService ttGoodsService;

    @Autowired
    SearchServiceClient searchServiceClient;

    @Autowired
    TtGoodsMapper ttGoodsMapper;

    //任务调度入口
    @XxlJob("GoodsPublishJobHandler")
    public void goodsPublishJobHandler() throws Exception{

        //分片参数
        int shardIndex = XxlJobHelper.getShardIndex();//执行器的序号
        int shardTotal = XxlJobHelper.getShardTotal();//执行器总数
        //调用抽象类的方法执行任务
        process(shardIndex,shardTotal,"goods_publish",30,60);

    }


    //执行课程发布任务的逻辑
    @Override
    public boolean execute(MqMessage mqMessage) {
        //从mqMessage中拿到商品id
        String goodsId = mqMessage.getBusinessKey1();
        //向elasticsearch写索引数据
        saveCourseIndex(mqMessage,goodsId);
        //向redis写数据
        //返回true表示任务完成
        return true;
    }

    /**
     * 保存商品索引信息，第二个阶段任务
     * @param mqMessage
     * @param goodsId
     */
    private void saveCourseIndex(MqMessage mqMessage,String goodsId){
        //任务id
        Long taskId = mqMessage.getId();
        MqMessageService mqMessageService = this.getMqMessageService();
        //任务幂等性处理，该方法被执行多次也只能有被执行一次的效果
        int stageTwo = mqMessageService.getStageTwo(taskId);
        if (stageTwo > 0) {
            log.debug("商品索引信息已引入，无需处理...");
            return;
        }
        //查询课程信息，调用搜索服务添加索引...
        //从课程发布表中查信息
        LambdaQueryWrapper<TtGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TtGoods::getId,goodsId);
        TtGoods ttGoods = ttGoodsMapper.selectOne(queryWrapper);
        GoodsIndex goodsIndex = new GoodsIndex();
        goodsIndex.setId(ttGoods.getId());
        goodsIndex.setGoodsName(ttGoods.getGoodsName());
        goodsIndex.setGoodsIntro(ttGoods.getGoodsIntro());
        goodsIndex.setPrice(ttGoods.getPrice());
        goodsIndex.setCount(ttGoods.getCount());
        goodsIndex.setUrl(ttGoods.getUrl());
        goodsIndex.setShopId(ttGoods.getShopId());
        //远程调用
        Boolean add = searchServiceClient.add(goodsIndex);
        if (!add) {
            TaoTaoException.cast("远程调用搜索服务添加商品索引失败");
        }
        //任务处理完成，将任务状态修改为已完成
        mqMessageService.completedStageOne(taskId);
    }
}
